import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

/**
 * 
 * this class is MangmentUI gui
 *
 */
public class MangmentUI extends JPanel {
	
/**
 *  role choose combobox
 */
	private JComboBox mngBox = null;
	/**
	 * label for role
	 */
	private JLabel mngOptLabel = null;
	/**
	 * label for password
	 */
	private JLabel lblPassword = null;
	/**
	 * Password Field
	 */
	private JPasswordField textField;
	/**
	 * label for Management LogIn
	 */
	private JLabel lblMangmentLogIn = null;
	/**
	 * login button
	 */
	private JButton btnLogIn = null;
	/**
	 *  back button
	 */
	public JButton btnBack = null;
	/**
	 *  help button
	 */
	public JButton btnHelp = null;
	private String optFlag = "Secretary";
	/**
	 *  AdminUI gui
	 */
	private AdminUI admUI = null;
	/**
	 *  ManUI gui
	 */
	private ManUI manUI = null;
	/**
	 *  SecUI gui
	 */
	private SecUI secUI = null;
	/**
	 *  ManagerUI gui
	 */
	private ManagerUI ManagerUI = null;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;

	
	public MangmentUI() {
		super();
		initialize();
	}


	private void initialize() {
		admUI = new AdminUI();
		manUI = new ManUI();
		secUI = new SecUI();
		ManagerUI = new ManagerUI();
		curPanel = this;
		
		/**********************    the back button **************************************/
		
		secUI.btnBack.addActionListener(new ActionListener() {   //back from secretary UI
			public void actionPerformed(ActionEvent arg0) {
				Global.main.setContentPane(curPanel);
				curPanel.setVisible(true);
			}
		});
		admUI.btnBack.addActionListener(new ActionListener() {   //back from admin UI
			public void actionPerformed(ActionEvent arg0) {
				Global.main.setContentPane(curPanel);
				curPanel.setVisible(true);
			}
		});
		manUI.btnBack.addActionListener(new ActionListener() {   //back from management UI
			public void actionPerformed(ActionEvent arg0) {
				Global.main.setContentPane(curPanel);
				curPanel.setVisible(true);
			}
		});
		ManagerUI.btnBack.addActionListener(new ActionListener() {   //back from manager UI
			public void actionPerformed(ActionEvent arg0) {
				Global.main.setContentPane(curPanel);
				curPanel.setVisible(true);
			}
		});
		
		
		/**********************    the back button **************************************/
		
		mngOptLabel = new JLabel();
		mngOptLabel.setBounds(new Rectangle(48, 71, 48, 19));
		mngOptLabel.setText("Role:");
	
		btnLogIn = new JButton();
		btnLogIn.setBounds(new Rectangle(141, 172, 95, 19));
		btnLogIn.setText("Log In");
		
		textField = new JPasswordField();
		textField.setBounds(126, 114, 95, 25);
		textField.setColumns(10);
		
		
		lblPassword = new JLabel("Password:");
		lblPassword.setBounds(48, 119, 76, 14);

		
		lblMangmentLogIn = new JLabel("Management Log In");
		lblMangmentLogIn.setBounds(176, 27, 133, 14);

		
		this.setSize(400, 321);
		this.setLayout(null);
		
		this.add(mngOptLabel);
		this.add(lblPassword);
		this.add(textField);
		this.add(lblMangmentLogIn);
		this.add(getLogIn(),null);
		this.add(getMngBox(), null);
		btnBack = new JButton("Back");
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose your Role and enter your password", "and then click on 'Login' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		this.setVisible(true);
		
		btnBack.setBounds(200, 285, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
	}
	
	private JComboBox getMngBox() {
		if (mngBox == null) {
			mngBox = new JComboBox();
			mngBox.setBounds(new Rectangle(126, 68, 95, 25));
			mngBox.addItem("Secretary");
			mngBox.addItem("Admin");
			mngBox.addItem("Mangment");
			mngBox.addItem("Manager");
			mngBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					optFlag = mngBox.getSelectedItem().toString();
					System.out.println("this is : " + optFlag); 
				}
			});
		}
		return mngBox;
	}
	
	private JButton getLogIn() {
		this.btnLogIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (optFlag.equals("Secretary"))
				{
					if ( textField.getText().equals("123") )
					{
						setVisible(false);
						System.out.println("now loged Secretary"); 
						secUI.setVisible(true);
						Global.main.setContentPane(secUI);	
					}
					else // wrong password
					{
						ErrWin err = new ErrWin ("The Password is incorrect");
						System.out.println("Error - wrong password");
					}
				}
				
				if (optFlag.equals("Mangment"))
				{
					if ( textField.getText().equals("456") )
					{
						setVisible(false);
						System.out.println("now loged Mangment"); 
						manUI.setVisible(true);
						Global.main.setContentPane(manUI);
					}
					else // wrong password
					{
						ErrWin err = new ErrWin ("Wrong Password");
						System.out.println("Error - wrong password");
					}
				}
				
				if (optFlag.equals("Manager"))
				{
					if ( textField.getText().equals("789") )
					{
						setVisible(false);
						System.out.println("now loged Manager");
						ManagerUI.setVisible(true);
						Global.main.setContentPane(ManagerUI);
						// open Manger menu
						
						
					}
					else // wrong password
					{
						ErrWin err = new ErrWin ("Wrong Password");
						System.out.println("Error - wrong password");
					}
				}
				
				if (optFlag.equals("Admin"))
				{
					if ( textField.getText().equals("000") )
					{
						setVisible(false);
						System.out.println("now loged Admin");
						admUI.setVisible(true);
						Global.main.setContentPane(admUI);
					}
					else // wrong password
					{
						ErrWin err = new ErrWin ("Wrong Password");
						System.out.println("Error - wrong password");
					}
				}
				textField.setText(null);
				
			}
		});
		return btnLogIn;
	}
}
